package com.greatwideweb.util
{
  import mx.collections.ArrayCollection;

  public class DateUtils
  {
    import mx.collections.ArrayCollection;
    
    private static var _months:ArrayCollection;
    public static function isWeekendHoliday(d:Date):Boolean
    {
      if(d.getDay() == 5 || d.getDay() == 6 || d.getDay() == 0)
        return true;
      else{
       if(isHoliday(d))
         return true;
       else
         return false;
      }
         
    }
    
    public static function isHoliday(d:Date):Boolean
    {
      var now:Date = new Date();
      var hDate:Date;
      var i:int=0;
      if(d.getMonth() == 6 && d.getDate() == 4)
        return true;
       
      if(isMemorialDay(d))
        return true; 
      
      if(isLaborDay(d))
        return true; 
      
      return false;
    }
    
    public static function isMemorialDay(d:Date):Boolean
    {
      var days:Array = getMemorialDays();
      for(var i:int=0;i< days.length;i++)
      {
        if(isDateEqual(d, days[i]))
        {
          return true;
        }  
      }
      
      return false;
    }
    
    public static function isLaborDay(d:Date):Boolean
    {
      var days:Array = getLaborDays();
      for(var i:int=0;i< days.length;i++)
      {
        if(isDateEqual(d, days[i]))
        {
          return true;
        }  
      }
      
      return false;
    }
    
    private static function getMemorialDays():Array
    {
      var result:Array = new Array();
      result.push(new Date(2011,4,30));
      result.push(new Date(2012,4,28));
      result.push(new Date(2013,4,27));
      result.push(new Date(2014,4,26));
      result.push(new Date(2015,4,15));
      result.push(new Date(2016,4,30));
      result.push(new Date(2017,4,29));
      result.push(new Date(2018,4,28));
      result.push(new Date(2019,4,27));
      result.push(new Date(2020,4,25));
      return result;
    }
    
    public static function getLaborDays():Array
    {
      var result:Array = new Array();
      result.push(new Date(2011,8,5));
      result.push(new Date(2012,8,3));
      result.push(new Date(2013,8,2));
      result.push(new Date(2014,8,1));
      result.push(new Date(2015,8,7));
      result.push(new Date(2016,8,5));
      result.push(new Date(2017,8,4));
      result.push(new Date(2018,8,3));
      result.push(new Date(2019,8,2));
      result.push(new Date(2020,8,7));
      return result;
    }
    
    public static function isDateEqual(d1:Date,d2:Date):Boolean
    {
      if(d1.getDate() == d2.getDate() && 
         d1.getFullYear() == d2.getFullYear() && 
         d1.getMonth() == d2.getMonth() )
         return true;
      else
        return false;
    }
    
    public static function getSQLLiteDate(d:Date):String
    {
      var mm:String = (d.getMonth() + 1).toString();
      if(mm.length == 1)
        mm = "0" + mm;
        
      var dd:String = String(Number(d.getDate()));
      if(dd.length == 1)
        dd = "0" + dd;
      return d.getFullYear() + "-" + mm + "-" + dd;
    }
    
    
    public static function getDisplayDate(d:Date):String
    {
      return d.date + "/" + (d.getMonth() + 1) + "/" + d.getFullYear();
    }
    
    public static function getLongDisplay(d:Date):String
    {
      
      return getMonthDescr(d.getMonth()) + " " + d.date + " " + d.getFullYear();
    }
    
    public static function getMonths():ArrayCollection
    {
     if(_months == null)
       _months = buildMonths();
       
      return _months;
    }
    
    public static function getMonthDescr(i:int):String
    {
      if(_months == null)
       _months = buildMonths();
       
       return _months[i].label;
    }
    
    public static function getMonthShortDescr(i:int):String
    {
      if(_months == null)
       _months = buildMonths();
       
       return _months[i].shortLabel;
    }
    
    public static function span(d1:Date,d2:Date):int
    {
      return dayYear(d2) - dayYear(d1);
      //d3.get
    }
    
    public static function dayYear(d:Date):int
    {
      var n:int = 0;
      var _d:Date = new Date (d.getFullYear(), 0, 1)
      while (_d.getTime() < d.getTime()) {_d.setDate(_d.getDate() + 1); n++}
      trace("Return Day of year " + d + " Day: " + n);
      return n;
    }
    
    private static function buildMonths():ArrayCollection
    {
    _months = new ArrayCollection();
    var o:Object = new Object();
     o.label = "January";
     o.shortLabel = "Jan";
     o.value = 0;
     _months.addItem(o);
     o= new Object();
     o.label = "February";
     o.shortLabel = "Feb";
     o.value = 1;
     _months.addItem(o);
     o= new Object();
     o.label = "March";
     o.shortLabel = "Mar";
     o.value = 2;
     _months.addItem(o);
      o= new Object();
     o.label = "April";
     o.shortLabel = "Apr";
     o.value = 3;
     _months.addItem(o);
      o= new Object();
     o.label = "May";
     o.shortLabel = "May";
     o.value = 4;
     _months.addItem(o);
      o= new Object();
     o.label = "June";
     o.shortLabel = "Jun";
     o.value = 5;
     _months.addItem(o);
      o= new Object();
     o.label = "July";
     o.shortLabel = "Jul";
     o.value = 6;
     _months.addItem(o);
      o= new Object();
     o.label = "August";
     o.shortLabel = "Aug";
     o.value = 7;
     _months.addItem(o);
      o= new Object();
     o.label = "September";
     o.shortLabel = "Sep";
     o.value = 8;
     _months.addItem(o);
      o= new Object();
     o.label = "October";
     o.shortLabel = "Oct";
     o.value = 9;
     _months.addItem(o);
      o= new Object();
     o.label = "November";
     o.shortLabel = "Nov";
     o.value = 10;
     _months.addItem(o);
      o= new Object();
     o.label = "December";
     o.shortLabel = "Dec";
     o.value = 11;
     _months.addItem(o);
     return _months;
    }
    
    public static function isDateGreater(d1:Date,d2:Date):Boolean
    {
      if(d1.getFullYear() < d2.getFullYear())
        return false;
      
      if(d1.getFullYear() == d2.getFullYear())
      {
        if(d1.getMonth() < d2.getMonth())
          return false;
          
        else if(d1.getMonth() == d2.getMonth()){
          
          if(d1.getDate() <= d2.getDate())
            return false;
          else
            return true;
        }else
          return true;
        
      }else{
        return true;
      }
    }
    
    public static function getDayDescr(d:int):String
  {
    var result:String ="";
    switch(d)
    {
      case 0:
      result ="Su";
      break;
      
      case 1:
      result ="Mo";
      break;
      
      case 2:
      result ="Tu";
      break;
      
      case 3:
      result ="We";
      break;
      
      case 4:
      result ="Th";
      break;
      
      case 5:
      result ="Fr";
      break;
      
      case 6:
      result ="Sa";
      break;
       
    }
    return result;
  }
  }
}